package com.h.ssRoom.controller;


import com.h.ssRoom.model.Appoints;
import com.h.ssRoom.model.Seats;
import com.h.ssRoom.repo.AppointsRepo;
import com.h.ssRoom.repo.SeatsRepo;
import com.h.ssRoom.util.SeatHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.sql.Timestamp;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(path = "/api",produces = "application/json; charset=utf-8")
public class SeatsController {
    @Autowired
    private SeatsRepo seatsRepo;
    private AppointsRepo appointsRepo;

    @RequestMapping(value = "/getSeatsByTime")
    public List<SeatHelper> getSeatsByTime(@RequestParam(value = "ssRoomID",required = true) String ssRoomID,
                                           @RequestParam(value = "beginTime",required = true) String beginTime,
                                           @RequestParam(value = "endTime",required = true) String endTime){

        List<Seats> seats = seatsRepo.getSeatsByRoom(ssRoomID);

        List<SeatHelper> seatForTime = null;

        //遍历seats传入seatHelper
        for(Seats s : seats){
            SeatHelper seat = new SeatHelper(s);
            seatForTime.add(seat);
        }
        System.out.println(seatForTime);

        //遍历每一个座位
        for(SeatHelper s : seatForTime){
            //查询座位订单
            List<Appoints> appoints = appointsRepo.getAppointsByssRoomIDAndseatID(s.getSsRoomID(),s.getSeatID());

            //遍历每一个座位订单
            for(Appoints A : appoints){
                //判断订单是否冲突
                String isColl = A.isCollision(beginTime,endTime);
                //不冲突
                if( isColl == "No"){
                    continue;
                }else{
                    //列出冲突时间
                    s.setUseTime(s.getUseTime() == null ? isColl:s.getUseTime() + "、" + isColl);
                }
            }
            if(s.getUseTime() == null){
                s.setUseful(true);
            }
        }

        return seatForTime;

    }

}
